﻿using Identity.Extensions;
using Identity.Service;
using Microsoft.AspNetCore.Authorization;
using WorkOrder.Extensions;
using WorkSystem.Components;
using Microsoft.AspNetCore.Routing;
using Serilog;
using System.Text.Json;
using System.Text.Encodings.Web;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http.Json;
using WorkOrder.Interfaces;
using WorkOrder.Services;
using Microsoft.Extensions.FileProviders;
using WorkSystem.Services;
var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorComponents()
    .AddInteractiveServerComponents()
    .AddInteractiveWebAssemblyComponents()
    .AddAuthenticationStateSerialization();

builder.Services.AddBootstrapBlazor();
builder.Services.AddBootstrapBlazorTableExportService();
#region SeriLog日志配置
Log.Logger = new LoggerConfiguration()  
    .ReadFrom.Configuration(builder.Configuration)
    .CreateLogger();
builder.Host.UseSerilog();
#endregion

#region Kestrel服务器配置
//! 配置IP地址和端口号
builder.WebHost.ConfigureKestrel((context, serverOptions) =>
{
    var kestrelSection = context.Configuration.GetSection("Kestrel");
    if (kestrelSection == null || !kestrelSection.Exists())
    {
        throw new Exception("Kestrel 配置未找到！");
    }

    Console.WriteLine("Kestrel 配置加载成功：");
   

    serverOptions.Configure(kestrelSection);
});
//! 配置IP地址和端口号
#endregion
builder.Services.Configure<JsonOptions>(options =>
{
    options.SerializerOptions.WriteIndented = true;
    options.SerializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
    options.SerializerOptions.PropertyNamingPolicy = null;
    options.SerializerOptions.DictionaryKeyPolicy = null;
    options.SerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
 
builder.Services.AddAdminSecurity<AdminService>(builder.Configuration);
builder.Services.AddWordOrderDataService(builder.Configuration);
builder.Services.AddMyIdentity(builder.Configuration);
//预留通知服务
// builder.Services.Configure<EmailConfig>(builder.Configuration.GetSection("Notification:Email"));
// builder.Services.Configure<SmsConfig>(builder.Configuration.GetSection("Notification:Sms"));
// builder.Services.Configure<WeChatConfig>(builder.Configuration.GetSection("Notification:WeChat"));
builder.Services.AddScoped<ISystemNotificationService, SystemNotificationService>();
var app = builder.Build();
await DatabaseInitializer.InitializeDatabaseAsync(app.Services,builder.Configuration);
await IdentityDatabaseInitializer.InitializeDatabaseAsync(app.Services,builder.Configuration);
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseWebAssemblyDebugging();
}
else
{
    app.UseExceptionHandler("/Error", createScopeForErrors: true);
}

app.UseBootstrapBlazor();
app.UseAntiforgery();
app.UseAuthentication(); // 添加身份验证中间件
app.UseAuthorization();  // 添加授权中间件
// 配置默认静态文件服务（wwwroot 文件夹）
app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "Upload")),
    RequestPath = "/Upload"
});

app.MapStaticAssets();
app.MapRazorComponents<App>()
    .AddInteractiveServerRenderMode()
    .AddInteractiveWebAssemblyRenderMode()
    .AddAdditionalAssemblies(typeof(WorkSystem.Client._Imports).Assembly)
    .AddAdditionalAssemblies(typeof(Identity._Imports).Assembly);
app.MapAdditionalIdentityEndpoints();
 
app.Run();
